//
//  MainTabView.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/30.
//  Copyright © 2018年 well. All rights reserved.
//

#import "MainTabView.h"


@implementation MainTabView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.btnImageArr = @[
                        @{@"ChoosenImage":@"HomeChoosen",@"DefaultImage":@"HomeDefault"},
                        @{@"ChoosenImage":@"DocumentChoosen",@"DefaultImage":@"DocumentDefault"},
                        @{@"ChoosenImage":@"CreateTaskChoosen",@"DefaultImage":@"CreateTaskDefault"},
                        @{@"ChoosenImage":@"CountChoosen",@"DefaultImage":@"CountDefault"},
                        @{@"ChoosenImage":@"MemberChoosen",@"DefaultImage":@"MemberDefault"}
                        ];
        
        [self initFrame];
    }
    return self;
}


-(void)initFrame
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithRed:144/255.0f green:148/255.0f blue:158/255.0f alpha:1.0f];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    for (int i = 0; i<5; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = 100 + i;
        NSDictionary *dic = [self.btnImageArr objectAtIndex:i];
        [btn setImage:[UIImage imageNamed:[dic valueForKey:@"ChoosenImage"]] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:[dic valueForKey:@"DefaultImage"]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(choosenTabButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(self);
            if (i == 0) {
                make.left.mas_equalTo(self);
                [self choosenTabButton:btn];
            }
            else
            {
                UIView *lastView = [self viewWithTag:100+i-1];
                make.left.mas_equalTo(lastView.mas_right);
                make.width.mas_equalTo(lastView);
            }
            
            if (i == 4) {
                make.right.mas_equalTo(self);
            }
        }];
    }
}

-(void)choosenTabButton:(UIButton *)sender
{
    if (sender.tag != 102) {
        if (self.choosenButton != nil) {
            self.choosenButton.selected = false;
        }
        
        self.choosenButton = sender;
        self.choosenButton.selected = true;
    }
    
    if ([self.delegate respondsToSelector:@selector(changeChoosen:)]) {
        [self.delegate changeChoosen:sender.tag];
    }
}


#pragma mark    懒加载
-(NSArray *)btnImageArr
{
    if(!_btnImageArr)
    {
        _btnImageArr = [NSArray array];
    }
    return _btnImageArr;
}

-(UIButton *)choosenButton
{
    if(!_choosenButton)
    {
        _choosenButton = [UIButton new];
    }
    return _choosenButton;
}

@end
